{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在python的函数中经常能看到输入的参数前面有一个或者两个星号：例如"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def foo(param1, *param2):\n",
    "def bar(param1, **param2):"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这两种用法其实都是用来将任意个数的参数导入到python函数中。\n",
    "\n",
    "# 单星号（*）：*agrs\n",
    "将所以参数以元组(tuple)的形式导入：\n",
    "例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "(2, 3, 4, 5)\n"
     ]
    }
   ],
   "source": [
    "def foo(param1, *param2):\n",
    "    print(param1)\n",
    "    print(param2)\n",
    "foo(1,2,3,4,5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 双星号（**）：**kwargs\n",
    "将参数以字典的形式导入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "{'a': 2, 'b': 3}\n"
     ]
    }
   ],
   "source": [
    "def bar(param1, **param2):\n",
    "    print(param1)\n",
    "    print(param2)\n",
    "bar(1,a=2,b=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "此外，单星号的另一个用法是解压参数列表："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2\n"
     ]
    }
   ],
   "source": [
    "def foo(bar, lee):\n",
    "    print(bar, lee)\n",
    "l = [1, 2]\n",
    "foo(*l)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当然这两个用法可以同时出现在一个函数中：例如"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "(3, 4)\n",
      "{'e': 5, 'f': 6, 'g': 7}\n"
     ]
    }
   ],
   "source": [
    "def foo(a, b=10, *args, **kwargs):\n",
    "    print(a)\n",
    "    print(b)\n",
    "    print(args)\n",
    "    print(kwargs)\n",
    "foo(1, 2, 3, 4, e=5, f=6, g=7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:root] *",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
